package test0ne;

import java.util.LinkedList;
import java.util.Queue;

public class TreeNode {
    public Integer val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(Integer val) {
        this.val = val;
    }

    public static TreeNode buildTreeNode(Integer[] arr) {
        Queue<TreeNode> list = new LinkedList<>();
        TreeNode root = new TreeNode(arr[0]);
        list.add(root);
        int index = 1;

        while (!list.isEmpty()) {
            TreeNode cur = list.poll();
            if(cur == null) {
                continue;
            }
            if(index+1 <= arr.length ) {
                cur.left = arr[index] == null? null:new TreeNode(arr[index]);
                index++;
                list.add(cur.left);
            }
            if(index+1 <= arr.length) {
                cur.right = arr[index] == null? null:new TreeNode(arr[index]);
                index++;
                list.add(cur.right);
            }
        }
        return root;
    }

    public static void printTreeNode(TreeNode root) {
        Queue<TreeNode> list = new LinkedList<>();
        list.add(root);

        StringBuilder stringBuilder = new StringBuilder();
        while (!list.isEmpty()) {
            TreeNode cur = list.poll();
            stringBuilder.append(cur.val).append(",");
            if(cur.left!=null) {
                list.add(cur.left);
            }
            if(cur.right!=null) {
                list.add(cur.right);
            }
        }
        System.out.println(stringBuilder.toString().substring(0, stringBuilder.length()-1));
    }
}
